import Mock from "mockjs";
import { param2Obj } from "../utils/myUtils";
let list = [];

for (let i = 0; i < 200; i++) {
	list.push({
		id: Mock.mock("@id"),
		location: Mock.mock("@county(true)"),
		name: Mock.mock("@cname()"),
		date: new Date().toLocaleDateString(),
		type: Mock.mock("@integer(0, 2)"),
		income: Mock.mock("@integer(1000, 50000)"),
		expend: Mock.mock("@integer(1000, 50000)"),
		money: Mock.mock("@integer(1000, 50000)"),
	});
}

export default {
	getHomeData(val) {
		let { keyWord } = param2Obj(val.url);
		keyWord ? "" : (keyWord = "");
		let newList = list.filter((el) => el.name.includes(keyWord));
		return {
			code: 200,
			success: 0,
			msg: "请求数据成功",
			data: newList,
		};
	},
	deleteHomeData(val) {
		let idList = JSON.parse(val.body).idList;
		let newList = list.filter((el) => {
			return !idList.includes(el.id);
		});
		list = newList;
		return {
			code: 200,
			success: 0,
			msg: "数据删除成功",
			data: null,
		};
	},
	addHomeData(val) {
		let data = JSON.parse(val.body);
		list.unshift({
			id: Mock.mock("@id"),
			location: data.location,
			name: data.name,
			date: new Date().toLocaleDateString(),
			type: data.type,
			income: data.income,
			expend: data.expend,
			money: data.money,
		});
		return {
			code: 200,
			success: 0,
			msg: "数据添加成功",
			data: null,
		};
	},
	updateHomeData(val) {
		let data = JSON.parse(val.body);
		list.forEach((item) => {
			if (item.id == data.id) {
				item.location = data.location;
				item.name = data.name;
				item.date = data.date;
				item.type = data.type;
				item.income = data.income;
				item.expend = data.expend;
				item.money = data.money;
			}
		});
		return {
			code: 200,
			success: 0,
			msg: "数据修改成功",
			data: null,
		};
	},
};
